﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sauces.Bussinees.Entities.Compras;
using Sauces.Repository.Compras.Service;

namespace Sauces.Repository.Compras
{
   public  class ProveedorRepository:MasterRepository,IProveedorRepository
    {
        public List<Proveedor> getAllProveedor()
        {
            var query = from p in Context.Proveedor
                        select p;

            return query.ToList();
        }

        public List<Proveedor> getAllProveedorByRazonSocial(string razonSocial)
        {
            var query = from p in Context.Proveedor
                        select p;

            if (razonSocial != null && razonSocial != "")
            {
                query = from p in query
                        where p.RazonSocial.ToUpper().Contains(razonSocial.ToUpper())
                        select p;
            }
            return query.ToList();
        }

        public List<Proveedor> getAllProveedorByNombre(string nombre)
        {
            var query = from p in Context.Proveedor
                        select p;

            if (nombre != null && nombre != "")
            {
                query = from p in query
                        where p.RazonSocial.ToUpper().Contains(nombre.ToUpper())
                        select p;
            }
            return query.ToList();
        }

        public Proveedor getFromProveedorById(int id)
        {
           return  Context.Proveedor.Find(id);
        }

        public void addProveedor(Proveedor proveedor)
        {
            Context.Proveedor.Add(proveedor);
            Context.SaveChanges();
        }

        public void upDateProveedor(Proveedor proveedor)
        {
            Context.Entry(proveedor).State = System.Data.EntityState.Modified;
            Context.SaveChanges();
            
        }

        public void removeProveedor(int id)
        {
            var existe = Context.Proveedor.Where(p => p.Id.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                Context.Proveedor.Remove(existe);
                Context.SaveChanges();
            }
        }


        public List<Proveedor> getAllProveedorByRazonSocialRuc(string criterio)
        {
            var query = from p in Context.Proveedor
                        select p;

            if (criterio != null && criterio != "")
            {
                query = from p in query
                        where p.RazonSocial.ToUpper().Contains(criterio.ToUpper())||
                        p.Ruc.ToUpper().Contains(criterio.ToUpper())

                        select p;
            }
            return query.ToList();
        }


        public Proveedor getFromProveedorByRuc(string ruc)
        {
            return Context.Proveedor.Where(p => p.Ruc.Equals(ruc)).SingleOrDefault();
        }
    }
}
